package com.leetcode;

import java.util.HashMap;
import java.util.Map;

public class CheckInclusion_567 {
    public static void main(String[] args) {
        String s1 = "abcdxabcde", s2 = "abcdeabcdx";
        System.out.println(checkInclusion1(s1, s2));

    }

    //    s1 ="abcdxabcde"
//    s2 ="abcdeabcdx"
    public static boolean checkInclusion1(String s1, String s2) {
        Map<Character, Integer> need = new HashMap<>();
        Map<Character, Integer> window = new HashMap<>();
        for (int i = 0; i < s1.length(); i++) {
            need.put(s1.charAt(i), need.getOrDefault(s1.charAt(i), 0) + 1);
        }
        int left = 0, right = 0, valid = 0;
        while (right < s2.length()) {
            char d = s2.charAt(right);
            right++;
            if (need.containsKey(d)) {
                window.put(d, window.getOrDefault(d, 0) + 1);
                if (need.get(d).equals(window.get(d))) {
                    valid++;
                    System.out.println(valid);
                    if (valid == need.size()) {
                        return true;
                    }
                }
                if (need.get(d) < window.get(d)) {
                    while (!need.get(d).equals(window.get(d))) {
                        char c = s2.charAt(left);
                        left++;
                        if (window.get(c).equals(need.get(c))) {
                            valid--;
                        }
                        window.put(c, window.get(c) - 1);
                    }
                }
            } else {
                left = right;
                window.clear();
                valid = 0;
            }
        }
        return false;
    }


    // 判断 s 中是否存在 t 的排列
    public boolean checkInclusion2(String t, String s) {
        Map<Character, Integer> need = new HashMap<>();
        Map<Character, Integer> window = new HashMap<>();
        for (char c : t.toCharArray()) {
            need.put(c, need.getOrDefault(c, 0) + 1);
        }

        int left = 0, right = 0;
        int valid = 0;
        while (right < s.length()) {
            char c = s.charAt(right);
            right++;
            // 进行窗口内数据的一系列更新
            if (need.containsKey(c)) {
                window.put(c, window.getOrDefault(c, 0) + 1);
                if (window.get(c).intValue() == need.get(c).intValue())
                    valid++;
            }

            // 判断左侧窗口是否要收缩
            while (right - left >= t.length()) {
                // 在这里判断是否找到了合法的子串
                if (valid == need.size())
                    return true;
                char d = s.charAt(left);
                left++;
                // 进行窗口内数据的一系列更新
                if (need.containsKey(d)) {
                    if (window.get(d).intValue() == need.get(d).intValue())
                        valid--;
                    window.put(d, window.get(d) - 1);
                }
            }
        }
        // 未找到符合条件的子串
        return false;
    }
}

